'use strict';

module.exports = app => {
    return class GrowService extends app.Service {
        constructor(ctx) {
            super(ctx);
        }

        /**
         * 根据监护人的编号获取监护人信息
         * @param id
         * @returns {Promise<void>}
         */
        async get({id}) {
            let url = this.app.urls('getTrajectory');
            this.app.logger.info(url);
            const result = await this.ctx.fetch(url, {
                method: 'get',
                dataType: 'json',
                data: {
                    id
                }
            });
            // this.app.logger.info(result.data);
            let data = result.data;

            if (data.code != 200){
                throw new Error(data.message ? data.message : 'error');
            }

            return data.data;
        }

        /**
         * 根据根据学生编号获取成长记录
         * @param studentId
         * @returns {Promise<void>}
         */

        async list() {
            let url = this.app.urls('trajectoryList');
            this.app.logger.info(url);
            const result = await this.ctx.fetch(url, {
                method: 'get',
                dataType: 'json',
                data: {}
            });
            // this.app.logger.info(result.data);
            let data = result.data;

            if (data.code != 200){
                throw new Error(data.message ? data.message : 'error');
            }

            return data.data;
        }

        /**
         * 添加成长记录
         * @param params
         * @returns {Promise<void>}
         */
        async add(params) {
            let url = this.app.urls('trajectoryAdd');
            this.app.logger.info(url);
            const result = await this.ctx.fetch(url, {
                method: 'post',
                dataType: 'json',
                data: params
            });
            // this.app.logger.info(result.data);
            let data = result.data;

            if (data.code != 200){
                throw new Error(data.message ? data.message : 'error');
            }

            return data.data;
        }

        /**
         * 根据id删除成长记录
         * @param id
         * @returns {Promise<void>}
         */
        async delete({id}) {
            let url = this.app.urls('trajectoryDelete');
            this.app.logger.info(url);
            const result = await this.ctx.fetch(url, {
                method: 'post',
                dataType: 'json',
                data: {
                    id
                }
            });
            // this.app.logger.info(result.data);
            let data = result.data;

            if (data.code != 200){
                throw new Error(data.message ? data.message : 'error');
            }

            return data.data;
        }



        /**
         * 帖子动态
         * @param id
         * @returns {Promise<void>}
         */




        async dynamic({id}) {
            let url = this.app.urls('dynamic');
            this.app.logger.info(url);
            const result = await this.ctx.fetch(url, {
                method: 'get',
                dataType: 'json',
                data: {
                    id
                }
            });
            this.app.logger.info(result.data);
            let data = result.data;

            if (data.code != 200){
                throw new Error(data.message ? data.message : 'error');
            }

            return data.data;
        }







    }

};
